package com.tanhua.server.controller;


import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.server.service.OperationService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.ViewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("operation")
@Slf4j
public class OperationController {

    @Autowired
    private OperationService operationService;


    /**
     * 运营日志首页展示
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("log/home")
    @NoAuthorization
    public ResponseEntity<PageResult> getOperations(@RequestHeader("page")Integer page,
                                                    @RequestHeader("pageSize") Integer pageSize){
        try {
            PageResult pageResult = operationService.getOperations(page,pageSize);
            if (ObjectUtil.isNotEmpty(pageResult)){
                return ResponseEntity.ok().body(pageResult);
            }
        } catch (Exception e) {
            log.error("运营日志首页查询出错" + e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 增加数据给数据库
     */
    @PostMapping("log/test")
    @NoAuthorization
    public void addMongo(){
       operationService.addMongo();
    }


    /**
     * 运营日志中的操作中的查看
     * @param param
     * @return
     */
    @PostMapping("log/view")
    @NoAuthorization
    public ResponseEntity<ViewVo> getView(@RequestBody Map<String, String> param){
        try {
            ViewVo viewVo = operationService.getView(param);
            if (ObjectUtil.isNotEmpty(viewVo)){
                return ResponseEntity.ok().body(viewVo);
            }
        } catch (Exception e) {
            log.error("运营日志查看操作出错" + e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("log/condition")
    @NoAuthorization
    public ResponseEntity<PageResult> getOperations(@RequestBody Map<String, String> param){
        try {
            PageResult pageResult = operationService.queyOperations(param);
            if (ObjectUtil.isNotEmpty(pageResult)){
                return ResponseEntity.ok().body(pageResult);
            }
        } catch (Exception e) {
            log.error("运营日志条件查询出错" + e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
